package com.example.vv.service;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.util.StopWatch;

import java.util.Arrays;
import java.util.Comparator;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

/**
 * 描述：// TODO
 *
 * @author w.dehai.2024/6/4.18:13
 */
@Slf4j
public class A {

    ThreadPoolExecutor pool = new ThreadPoolExecutor(1, 1, 0, TimeUnit.SECONDS, new LinkedBlockingQueue<>(1));

    @Test
    void dddTest() {
        Integer[] array = Stream.of(1, 3, 2).sorted(Comparator.reverseOrder()).toArray(Integer[]::new);
        System.err.println(Arrays.toString(array));
    }

    @Test
    void mmTest() throws Exception {

        StopWatch sw = new StopWatch();
        sw.start();
        TimeUnit.SECONDS.sleep(2L);
        sw.stop();
        sw.start();
        TimeUnit.SECONDS.sleep(3L);
        sw.stop();
        System.err.println(sw.prettyPrint());


        CompletableFuture<Void> c = CompletableFuture.runAsync(() -> {
            CompletableFuture.runAsync(() -> {
                try {
                    TimeUnit.SECONDS.sleep(2L);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.err.println("inner: " + Thread.currentThread().getId());
            }, pool);
            System.err.println("outer.");
        }, pool);
        CompletableFuture.allOf(c).join();
        System.err.println("end.");

        CompletableFuture.supplyAsync(() -> {
            System.err.println(123);
            return "m";
        }).handle((t, d) -> {
            System.err.println(t);
            System.err.println(d);
            return null;
        }).exceptionally(t -> {
            return "";
        });
    }

    @Test
    void nnTest() {
        CompletableFuture<String> c = CompletableFuture.supplyAsync(() -> {
            try {
                return CompletableFuture.supplyAsync(() -> {
                    System.err.println("inner: " + Thread.currentThread().getId());
                    return "m";
                }, pool).get();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }, pool);
        CompletableFuture.allOf(c).join();
        System.err.println("end.");
    }
}
